package com.ct.dao.common;

import org.springframework.transaction.annotation.Transactional;
import org.hibernate.Query;

import java.io.Serializable;
import java.util.Collection;

import com.ct.model.common.Entity;

/**
 * Created by IntelliJ IDEA.
 * User: nmkravchenko
 * Date: 22.10.2009
 * Time: 17:23:14
 * To change this template use File | Settings | File Templates.
 */
public interface Dao<PKType extends Serializable, E extends Entity<PKType>> {
    /**
     * Save new entity.
     * @param aEntity entity to save
     */
    @Transactional
    void save(E aEntity);

    /**
     * Update old entity.
     * @param aEntity entity to update
     */
    @Transactional
    void update(E aEntity);

    /**
     * Save or Update entity depending on it's lifecycle state.
     * @param aEntity entity to update
     */
    @Transactional
    void saveOrUpdate(E aEntity);

    /**
     * Persist new entity.
     * <br><b>WARNING:</b>differs from #save by semantics.
     * <p>
     * persist() is well defined. It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. The spec doesn't say that, which is the problem I have with persist().
     * persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.
     * A method like persist() is required.
     * save() does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.
     *
     * @param aEntity entity to persist. Differs from save as persists.
     */
    @Transactional
    void persist(E aEntity);

    /**
     * Update old entity.
     * @param aEntity entity to merge
     * @return entity, that is loaded in current transaction and merged with given aEntity
     */
    @Transactional
    E merge(E aEntity);

    /**
     * Delete entity.
     * @param aEntity to delete
     */
    @Transactional
    void delete(E aEntity);

    /**
     * Sync in-memory entity instance with database values. Namely reread.
     *
     * @param aEntity a entity to refresh values of
     */
    void refresh(E aEntity);

    public Collection<E> findAll();

    public Collection<E> findAll(Class<E> aDictionaryClass);
}
